//
//  MVPlayViewController.m
//  LastTemp
//
//  Created by dllo on 15/4/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MVPlayViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MVPlayViewController ()

@end

@implementation MVPlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    NSURL *url = [NSURL URLWithString:self.mvPassURL];
//    MPMoviePlayerController *mvMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
//    [mvMoviePlayer.view setFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49)];
//    [self.view addSubview:mvMoviePlayer.view];
//    [mvMoviePlayer setFullscreen:YES animated:YES]; // 切换全屏模式
//    [mvMoviePlayer play];
////     使用NSNotificationCenter类注册一个观察者, 该观察者将监视来自对象mvMoviePlayer的特定通知, 并在收到这种特定通知的时候调用指定的方法
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playMovieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:mvMoviePlayer];
//
    MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:[playerViewController moviePlayer]];
    [self.view addSubview:playerViewController.view];
    MPMoviePlayerController *player = [playerViewController moviePlayer];
    [player play];
    [playerViewController release];
//    self.tabBarController.tabBar.hidden = YES;
//    self.navigationController.navigationBarHidden = YES;
    
    
    
    // 横屏
    //设置应用程序的状态栏到指定的方向
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
    //view旋转
    [self.view setTransform:CGAffineTransformMakeRotation(M_PI/2)];
    //    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    
    // 隐藏状态栏
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
        // iOS 7
        [self prefersStatusBarHidden];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
    
    
}


// 横屏
- (BOOL)prefersStatusBarHidden
{
    return YES;//隐藏为YES，显示为NO
}

-(void)viewWillAppear:(BOOL)animated
{
    //隐藏navigationController
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    
    //隐藏状态栏
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];}

- (void)viewWillDisappear:(BOOL)animated
{
    //显示状态栏
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
    //显示navigationController
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
}
- (void)movieFinishedCallback:(NSNotification *)aNotification{
    MPMoviePlayerController *player = [aNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
    [player stop];
//    self.tabBarController.tabBar.hidden = NO;
//    self.navigationController.navigationBarHidden = NO;
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

//- (void)playMovieFinished:(NSNotification *)theNotification{
//    MPMoviePlayerController *mvMoviePlayer = [theNotification object];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:mvMoviePlayer];
//    [mvMoviePlayer.view removeFromSuperview];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
